package com.doge.walknovel.business.reader.viewholders

import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.doge.wnpro.R
import com.doge.walknovel.api.model.ChapterInfo
import com.doge.walknovel.business.reader.UNLOCK
import com.doge.walknovel.business.reader.menus.ReaderHelper
import com.doge.walknovel.core.base.adapters.AdapterCallback
import com.doge.walknovel.core.base.isRefreshing
import com.doge.wnpro.databinding.CellLockedBinding
import com.doge.walknovel.utils.getColor
import com.doge.walknovel.utils.getString
import com.doge.walknovel.utils.html
import com.doge.walknovel.utils.setOnClickCallback

class LockedViewHolder(
    parent: ViewGroup,
    callback: AdapterCallback,
    private val binding: CellLockedBinding = CellLockedBinding.inflate(
        LayoutInflater.from(parent.context), parent, false
    ),
) : RecyclerView.ViewHolder(binding.root) {

    init {
        binding.unlock.setOnClickCallback {
            callback.invoke(UNLOCK, itemView.tag)
        }
    }

    fun bind(item: ChapterInfo) {
        itemView.tag = item
        itemView.updateLayoutParams { height = if (item.locked) MATCH_PARENT else 0 }
        binding.chapterName.text = item.chapterTitle.html
        binding.chapterName.typeface = item.appTypeface
        binding.text.text = item.preview.html
        binding.text.textSize = ReaderHelper.size.toFloat()
        binding.text.setTextColor(getColor(if (ReaderHelper.light) "#333333" else "#999999"))
        binding.text.typeface = item.appTypeface
        binding.unlockPrice.text = String.format(
            getString(R.string.xxx_coins_to_unlock_this_chapter),
            item.price.toString()
        )
        binding.shadow.setBackgroundResource(when {
            !ReaderHelper.light -> R.drawable.shape_shadow_bottom_121212
            ReaderHelper.bgColor == ReaderHelper.bgGreen -> R.drawable.shape_shadow_bottom_e0ecd9
            ReaderHelper.bgColor == ReaderHelper.bgYellow -> R.drawable.shape_shadow_bottom_ffeed4
            else -> R.drawable.shape_shadow_bottom_ffffffff
        })
        binding.loadingUnlock.isVisible = item.appUnlocking.isRefreshing()
        binding.unlock.isEnabled = !item.appUnlocking.isRefreshing()
    }
}